<template>
  <div class="updateBirthday">
    <van-datetime-picker
      v-model="currentDate"
      type="date"
      :min-date="minDate"
      :max-date="maxDate"
      @cncel="$emit('close')"
      @confirm="onConfirm"
    />
  </div>
</template>

<script>
import { updateUserProfile } from "../api/user.js";
import dayjs from "dayjs";
export default {
  name: "updateBirthday",
  props: {
    value: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      minDate: new Date(1970, 0, 1),
      maxDate: new Date(),
      currentDate: new Date(this.value)
    };
  },
  methods: {
    async onConfirm() {
      this.$toast.loading({
        message: "保存中",
        forbidclick: true
      });

      // const date = `${this.currentDate.getFullYear()}-${this.currentDate.getMonth() + 1}-${this.currentDate.getDate()}`
      // 提交更新用户生日
      const date = dayjs(this.currentDate).format("YYYY-MM-DD");
      await updateUserProfile({
        birthday: date
      });
      this.$toast.success("保存成功");
      // 直接让父级更新不用刷新  发布input事件，更新父组件v-model绑定的数据
      this.$emit("input", date);
      // 关闭弹出层
      this.$emit("close");
    }
  }
};
</script>
<style scoped></style>
